/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ChatSystem;


import common.*;
import common.Fichier;
import common.Hello;
import common.Text;
import common.TransfertFileAccept;
import common.TransfertFileRequest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.InetAddress;

/**
 *
 * @author doutre
 */
public class MessageFactory {
    
    public static byte[] toByte(Message mess) {

        ByteArrayOutputStream tab = new ByteArrayOutputStream();
        ObjectOutput oo = null;
        byte[] classe = null;
        try {
            //System.out.println("Ecriture dans un array");
            oo = new ObjectOutputStream(tab);

            oo.writeObject(mess);
            oo.flush();
            classe = tab.toByteArray();


            oo.close();


        } catch (Exception e) {
            System.err.println(e);
        }
        return classe;
    }


    public static Hello createHello(String username,Boolean reply) {
        Hello mess = null;
        try {
            mess = new Hello(username, InetAddress.getLocalHost(),reply);
        } catch (Exception e) {
            System.err.println(e);
        }
        return mess;
    }

    public static Bye createBye(){
        Bye mess = null;
        try {
            mess = new Bye(InetAddress.getLocalHost());
        } catch (Exception e) {
            System.err.println(e);
        }
        return mess;
    }
    

    public static Text createText(String msg){
        Text mess = null;
        try {
            mess = new Text(InetAddress.getLocalHost(),msg);
        } catch (Exception e) {
            System.err.println(e);
        }
        return mess;
    }


    public static Fichier createFichier(File fic){
        Fichier mess = null;
        try {
            mess = new Fichier(InetAddress.getLocalHost(),fic);
        } catch (Exception e) {
            System.err.println(e);
        }
        return mess;
    }

    public static TransfertFileAccept createTransfertFileAccept(Boolean acc){
        TransfertFileAccept mess = null;
        try {
            mess = new TransfertFileAccept(InetAddress.getLocalHost(),acc);
        } catch (Exception e) {
            System.err.println(e);
        }
        return mess;
    }

    public static TransfertFileRequest createTransfertFileRequest(String nom, int taille){
        TransfertFileRequest mess = null;
        try {
            mess = new TransfertFileRequest(InetAddress.getLocalHost(),nom,taille);
        } catch (Exception e) {
            System.err.println(e);
        }
        return mess;
    }
}
